#include "almListModel.h"
#include "ColumnConfig.h"
#include <QClipboard>
//#include <QSettings>
//#include <QDir>
//#include <QtCore>
//#include "../../ColumnConfig.h"

AlmListModel::AlmListModel(QObject *parent)
    :QAbstractTableModel(parent)
{
    m_bShowCheck = false;
}

AlmListModel::~AlmListModel()
{

}

//返回行数
int AlmListModel::rowCount(const QModelIndex & /* parent */) const
{
//    qDebug()<<" alm model rowCount:"<<m_almData.size();
    return m_almData.size();
}
//返回列数
int AlmListModel::columnCount(const QModelIndex & /* parent */) const
{
//    qDebug()<<" alm model columnCount:"<<m_roleNames.size();
    return m_roleNames.size();
}

//返回一个项的任意角色的值，这个项被指定为QModelIndex
QVariant AlmListModel::data(const QModelIndex &index, int role) const
{
	
    if (!index.isValid() || index.row() >= m_almData.size())
        return QVariant();

//    qDebug()<<index.row()<<" show check  role:"<<m_roleNames[role];
    if (m_bShowCheck && "CheckCol" == m_roleNames[role] && index.row()< m_checkList.size())
    {
        return m_checkList[index.row()];
    }

	ArrayListMsg almRow = m_almData.GetValue(index.row());
    int nPos = m_dataIndex.indexOf(m_roleNames[role]);
    if (nPos < 0 || nPos >= almRow.size())
        return QVariant();

    return almRow.GetQValue(nPos);
}


QHash<int, QByteArray> AlmListModel::roleNames() const
{
    return m_roleNames;
}


QList<int> AlmListModel::getColumnWidths()
{
    QList<int> list;
    QHash<int,QByteArray>::iterator it = m_roleNames.begin();
    while(it != m_roleNames.end())
    {
        list.append(120);
    }

    return list;
}

QVariant AlmListModel::getCellText(int nRow, int nCol)
{
    if (nRow >= 0 && nRow < m_almData.size())
    {
        ArrayListMsg almRow = m_almData.GetValue(nRow);
        if (nCol >= 0 && nCol < almRow.size())
            return almRow.GetQValue(nCol);
    }

    return QVariant();
}
#include "qtquickcontrolsapplication.h"
QVariant AlmListModel::copyCellText(int nRow, int nCol)
{
    QClipboard *clipboard = QtQuickControlsApplication::clipboard();   //获取系统剪贴板指针
//    QString originalText = clipboard->text();	     //获取剪贴板上文本信息
    int nPos = m_dataIndex.indexOf(m_roleNames[Qt::UserRole+nCol]);
    clipboard->setText(getCellText(nRow,nPos).toString());		             //设置剪贴板内容</span>

}

void AlmListModel::InitRoleNames(const QString &sListName)
{
    CColumnConfig set;
    set.GetRoleNames(sListName, m_roleNames, m_dataIndex);
    qDebug()<<sListName<<" role size:"<<m_roleNames.size();
}

void AlmListModel::InitAlmData( const ArrayListMsg& htm )
{
    m_almData = htm;

}

//m_roleNames最后1个role
void AlmListModel::setShowCheck(bool bShow,bool bDefaultCheck)
{
    m_bShowCheck = bShow;
    if (bShow)
    {
        qDebug()<<" ini check row:"<<m_almData.size();
//        beginResetModel();
        m_roleNames.insert(Qt::UserRole+m_roleNames.size()+1, "CheckCol");
        for (int i = 0; i < m_almData.size(); ++i)
        {
            m_checkList.push_back(bDefaultCheck);
        }
//        endResetModel();
    }
}

void AlmListModel::setCheck(int nRow, bool bCheck)
{
    qDebug()<<nRow<<" setCheck "<<bCheck<<" row:"<<m_checkList.size();
//    beginResetModel();
    if (nRow >= 0 && nRow < m_checkList.size())
        m_checkList[nRow] = bCheck;
    else
    {
        for (int i = 0; i < m_checkList.size(); ++i)
        {
            m_checkList[i] = bCheck;
        }
    }

//    endResetModel();
}

void AlmListModel::resetModel()
{
    beginResetModel();
    endResetModel();
}

bool AlmListModel::getCheck(int nRow)
{
    if (nRow >= 0 && nRow < m_checkList.size())
        return m_checkList[nRow] ;

    return false;
}

